import Vue from 'vue'
import Vuex from 'vuex'
import plugin from './plugins/axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    version: localStorage.getItem('version') ?
      localStorage.getItem('version') :
      'DST',
    isLogin: sessionStorage.getItem('isLogin')
  },
  mutations: {
    /**
     * 切换版本
     * @param state
     * @param version
     */
    changeVersion(state, version) {
      localStorage.setItem('version', version)
      state.version = version
    },
    /**
     * 切换登录状态
     * @param state
     * @param isLogin
     */
    changeLoginStatus(state, isLogin) {
      sessionStorage.setItem('isLogin', isLogin)
      state.isLogin = isLogin
    }
  },
  actions: {
    getTags({
      commit,
      state
    }, payload) {
      console.log(commit)
      return new Promise(async resolve => {
        let res = await plugin.axios.get('tags', {
          params: {
            type: payload.kind,
            mid: payload.mid,
            version: state.version
          }
        })
        resolve(res.data)
      })
    }
  }
})